//package cn.xfdzx.android.apps.shop.fragment.home;
//
//import android.content.Context;
//import android.content.Intent;
//import android.graphics.Color;
//import android.graphics.drawable.ColorDrawable;
//import android.os.Bundle;
//import android.view.View;
//import android.widget.AdapterView;
//import android.widget.ImageView;
//import android.widget.Toast;
//
//import androidx.annotation.NonNull;
//import androidx.recyclerview.widget.DefaultItemAnimator;
//import androidx.recyclerview.widget.GridLayoutManager;
//import androidx.recyclerview.widget.LinearLayoutManager;
//import androidx.recyclerview.widget.RecyclerView;
//
//import com.bumptech.glide.Glide;
//import com.bumptech.glide.load.resource.bitmap.CenterCrop;
//import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
//import com.bumptech.glide.request.RequestOptions;
//import com.scwang.smartrefresh.layout.SmartRefreshLayout;
//import com.scwang.smartrefresh.layout.api.RefreshLayout;
//import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
//import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
//import com.youth.banner.Banner;
//import com.youth.banner.BannerConfig;
//import com.youth.banner.Transformer;
//import com.youth.banner.listener.OnBannerListener;
//import com.youth.banner.loader.ImageLoader;
//
//import org.greenrobot.eventbus.EventBus;
//import org.greenrobot.eventbus.Subscribe;
//import org.greenrobot.eventbus.ThreadMode;
//
//import java.util.ArrayList;
//import java.util.List;
//
//import cn.xfdzx.android.apps.shop.R;
//import cn.xfdzx.android.apps.shop.activity.ClassMarketPurchaseActivity;
//import cn.xfdzx.android.apps.shop.activity.XFDGoodsDeatailsActivity;
//import cn.xfdzx.android.apps.shop.activity.XFDShopDetailsActivity;
//import cn.xfdzx.android.apps.shop.activity.login.OneLoginMainActivity;
//import cn.xfdzx.android.apps.shop.adapter.FineSelectXfdAdapter;
//import cn.xfdzx.android.apps.shop.adapter.FineSelectXfdNewAdapter;
//import cn.xfdzx.android.apps.shop.adapter.MarketClassGVAdapter;
//import cn.xfdzx.android.apps.shop.adapter.XfdTimeZhiNanAdapter;
//import cn.xfdzx.android.apps.shop.apps.Constants;
//import cn.xfdzx.android.apps.shop.base.BaseMvpFragment;
//import cn.xfdzx.android.apps.shop.bean.BaseObjectBean;
//import cn.xfdzx.android.apps.shop.bean.ConfirmGoodBeen;
//import cn.xfdzx.android.apps.shop.bean.HomeMarketInitDataBeen;
//import cn.xfdzx.android.apps.shop.bean.HomeMarketSelectBeen;
//import cn.xfdzx.android.apps.shop.bean.SelfOwnedShopBeen;
//import cn.xfdzx.android.apps.shop.contract.HomeMarketContract;
//import cn.xfdzx.android.apps.shop.contract.ItemClickListener;
//import cn.xfdzx.android.apps.shop.evenbus.HomeInitDataMessage;
//import cn.xfdzx.android.apps.shop.evenbus.UpdateCarNumMessage;
//import cn.xfdzx.android.apps.shop.fragment.OneFragment;
//import cn.xfdzx.android.apps.shop.presenter.HomeMarketPresenter;
//import cn.xfdzx.android.apps.shop.util.ItemOffsetDecoration;
//import cn.xfdzx.android.apps.shop.util.ProgressDialog;
//import cn.xfdzx.android.apps.shop.util.ScrollSpeedLinearLayoutManger;
//import cn.xfdzx.android.apps.shop.util.UtilsData;
//import cn.xfdzx.android.apps.shop.view.AutoPollRecyclerView;
//import cn.xfdzx.android.apps.shop.view.MyGridView;
//
//import static cn.xfdzx.android.apps.shop.apps.Constants.APP_TOKEN;
//
///**
// * @author gzk
// * @date 2020/3/26 0026
// * description：新发地市场采购
// */
//public class HomeMarketFragment extends BaseMvpFragment<HomeMarketPresenter> implements HomeMarketContract.View, OnBannerListener {
//
//    Banner banner;
//    MyGridView gvMarketClass;//中间显示5个分类
//    ImageView ivMarketActivity;//活动图
//    AutoPollRecyclerView aprvView;//
//    RecyclerView rvXfdGoodsSelect;//精选货源
//    SmartRefreshLayout mRefreshLayout;
//    private List<HomeMarketSelectBeen.DataBean> selectList = new ArrayList<>();
//    private int page = 1;
//    private int limit = Constants.LIMIT;
//
//
//    private MarketClassGVAdapter marketClassGVAdapter;
//    private XfdTimeZhiNanAdapter zhiNanAdapter;
//    //  private FineSelectXfdAdapter fineSelectXfdAdapter;
//
//    private FineSelectXfdNewAdapter fineSelectXfdAdapter;
//
//    @Override
//    protected void initView(View view) {
//        EventBus.getDefault().register(this);
//
//        banner = view.findViewById(R.id.ll_market_banner);
//        gvMarketClass = view.findViewById(R.id.gv_market_class);
//        ivMarketActivity = view.findViewById(R.id.iv_market_activity);
//        aprvView = view.findViewById(R.id.aprv_view);
//        rvXfdGoodsSelect = view.findViewById(R.id.rv_xfd_goods_select);
//        mRefreshLayout = view.findViewById(R.id.refreshLayout);
//
//        mPresenter = new HomeMarketPresenter();
//        mPresenter.attachView(this);
//
//        init();
//    }
//
//    @Override
//    protected int getLayoutId() {
//        return R.layout.fragment_home_market;
//    }
//
//
//    private void init() {
//        //设置内置样式，共有六种可以点入方法内逐一体验使用。
//        banner.setBannerStyle(BannerConfig.NOT_INDICATOR);
//        //设置图片加载器，图片加载器在下方
//        banner.setImageLoader(new MyLoader());
//
//        //设置轮播的动画效果，内含多种特效，可点入方法内查找后内逐一体验
//        banner.setBannerAnimation(Transformer.Default);
////
//        //设置轮播间隔时间
//        banner.setDelayTime(3000);
////
//        //设置是否为自动轮播，默认是“是”。
//        banner.isAutoPlay(true);
////        //设置指示器的位置，小点点，左中右。
////        banner.setIndicatorGravity(BannerConfig.CENTER)
////                //以上内容都可写成链式布局，这是轮播图的监听。比较重要。方法在下面。
////                .setOnBannerListener(this);
//
//
//        marketClassGVAdapter = new MarketClassGVAdapter(getActivity());
//        gvMarketClass.setAdapter(marketClassGVAdapter);
//        gvMarketClass.setSelector(new ColorDrawable(Color.TRANSPARENT));
//
//        //广告图
//        ivMarketActivity.setBackgroundResource(R.drawable.image_shichang_center);
//
//        zhiNanAdapter = new XfdTimeZhiNanAdapter(getContext());
//        LinearLayoutManager layoutManager = new ScrollSpeedLinearLayoutManger(getActivity());
//        layoutManager.setSmoothScrollbarEnabled(true);
//        layoutManager.setAutoMeasureEnabled(true);
//        aprvView.setLayoutManager(layoutManager);// 布局管理器。
//        aprvView.setHasFixedSize(true);// 如果Item够简单，高度是确定的，打开FixSize将提高性能。
//        aprvView.setItemAnimator(new DefaultItemAnimator());// 设置Item默认动画，加也行，不加也行。
//        aprvView.setAdapter(zhiNanAdapter);
//
//        rvXfdGoodsSelect.setItemAnimator(new DefaultItemAnimator());
//        GridLayoutManager mLayoutManager = new GridLayoutManager(getActivity(), 2);
//        rvXfdGoodsSelect.setLayoutManager(mLayoutManager);
//        ItemOffsetDecoration itemDecoration = new ItemOffsetDecoration(getContext(), R.dimen.item_offset);
//        rvXfdGoodsSelect.addItemDecoration(itemDecoration);
//        rvXfdGoodsSelect.setNestedScrollingEnabled(false);
////        fineSelectXfdAdapter = new FineSelectXfdAdapter(getActivity(), selectList, mPresenter);
////        fineSelectXfdAdapter.setOnItemClickListener(new ItemClickListener() {
////            @Override
////            public void onItemClick(int position) {
////                Intent intent = new Intent(getContext(), XFDGoodsDeatailsActivity.class);
////                intent.putExtra("good_id", selectList.get(position).getId() + "");
////                startActivity(intent);
////            }
////        });
//        fineSelectXfdAdapter = new FineSelectXfdNewAdapter(getActivity(), selectList, mPresenter);
//        rvXfdGoodsSelect.setAdapter(fineSelectXfdAdapter);
//
//        mRefreshLayout.setOnRefreshListener(new OnRefreshListener() {
//            @Override
//            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
//                page = 1;
//                mPresenter.initData();
//                mPresenter.initSelectData(true, true, page + "", limit + "");
//
//            }
//        });
//
//    }
//
//    @Override
//    public void onStart() {
//        super.onStart();
//        //开始轮播
//        banner.startAutoPlay();
//    }
//
//    @Override
//    public void onStop() {
//        //结束轮播
//        banner.stopAutoPlay();
//        super.onStop();
//    }
//
//    @Override
//    public void showLoading() {
//        if (OneFragment.TYPE_IS_RETAIL == 1) {
//            if (!ProgressDialog.getInstance().materialDialog.isShowing()) {
//                ProgressDialog.getInstance().show(getActivity());
//            }
//        }
//    }
//
//    @Override
//    public void hideLoading() {
//        if (OneFragment.TYPE_IS_RETAIL == 1) {
//            if (ProgressDialog.getInstance().materialDialog.isShowing()) {
//                ProgressDialog.getInstance().dismiss();
//            }
//        }
//    }
//
//    @Subscribe(threadMode = ThreadMode.MAIN)
//    public void XXX(HomeInitDataMessage messageEvent) {
//        if (messageEvent.getMessage() == 1) {
//            if (selectList.size()==0) {
//                mPresenter.initData();
//                mPresenter.initSelectData(true, false, page + "", limit + "");
//            }
//        }
//    }
//
//    @Override
//    public void onError(Throwable throwable) {
//        Toast.makeText(getActivity(), getResources().getString(R.string.the_server_is_busy_please_try_again_later), Toast.LENGTH_SHORT).show();
//    }
//
//    @Override
//    public void onInitDataSuccess(BaseObjectBean<HomeMarketInitDataBeen> bean) {
//        if (bean.getStatus() == Constants.SUCCESS_CODE) {
//            initBanner(bean.getData());
//            initCategory(bean.getData());
//            initZhiNan(bean.getData());
//        } else {
//            Toast.makeText(getActivity(), bean.getMsg(), Toast.LENGTH_SHORT).show();
//        }
//
//    }
//
//    private void initZhiNan(HomeMarketInitDataBeen data) {
//
//        zhiNanAdapter.setData(data.getXfdPriceArray());
//        aprvView.start();
//
//    }
//
//    private void initCategory(final HomeMarketInitDataBeen result) {
//        marketClassGVAdapter.setData((ArrayList<HomeMarketInitDataBeen.IndexWholesaleCategoryBean>) result.getIndexWholesaleCategory());
//        gvMarketClass.setOnItemClickListener(new AdapterView.OnItemClickListener() {
//            @Override
//            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
//                Intent intent = new Intent(getActivity(), ClassMarketPurchaseActivity.class);
//                intent.putExtra("title_name", result.getIndexWholesaleCategory().get(i).getName());
//                intent.putExtra("id", result.getIndexWholesaleCategory().get(i).getId() + "");
//                getActivity().startActivity(intent);
//            }
//        });
//    }
//
//    @Override
//    public void onResume() {
//        super.onResume();
//        if (!aprvView.isStart()) {
//            aprvView.start();
//        }
//
//    }
//
//    @Override
//    public void onPause() {
//        if (aprvView.isStart()) {
//            aprvView.stop();
//        }
//
//        super.onPause();
//    }
//
//    private void initBanner(HomeMarketInitDataBeen bean) {
//        List<String> imageUrls = new ArrayList<>();
//        for (int i = 0; i < bean.getBannersWholesale().size(); i++) {
//            imageUrls.add(bean.getBannersWholesale().get(i).getPic());
//        }
//        //设置图片网址或地址的集合
//        banner.setImages(imageUrls);
//
//        //必须最后调用的方法，启动轮播图。
//        banner.start();
//
//    }
//
//    @Override
//    public void onInitSelectDataSuccess(HomeMarketSelectBeen bean) {
//        if (bean.getStatus() == Constants.SUCCESS_CODE) {
//
//            if (page == 1) {
//                selectList.clear();
//                selectList = bean.getData();
//                fineSelectXfdAdapter.refreshDatas(selectList);
//                mRefreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
//                    @Override
//                    public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
//                        mPresenter.initSelectData(false, false, page + "", limit + "");
//
//                    }
//                });
//            }else {
//                selectList = bean.getData();
//                fineSelectXfdAdapter.addDatas(selectList);
//            }
//            if (bean.getData().size() == 0) {
//                mRefreshLayout.finishLoadMoreWithNoMoreData();
//            } else {
//                page++;
//            }
//
//            // selectList.addAll(bean.getData());
//            //  fineSelectXfdAdapter.notifyDataSetChanged();
//        } else {
//            Toast.makeText(getActivity(), bean.getMsg(), Toast.LENGTH_SHORT).show();
//        }
//    }
//
//    @Override
//    public void onSelfOwnedShopSuccess(SelfOwnedShopBeen bean) {
//
//    }
//
//    @Override
//    public void onAddCarSuccess(ConfirmGoodBeen bean, int position, int total_num) {
//
//    }
//
//    @Override
//    public void onAddCarSuccess(ConfirmGoodBeen bean,int position) {
//        if (bean.getStatus() == Constants.SUCCESS_CODE) {
//            selectList.get(position).setLinshi_num(selectList.get(position).getLinshi_num()+1);
//            fineSelectXfdAdapter.notifyDataSetChanged();
//            Toast.makeText(getActivity(), "加入购物车成功", Toast.LENGTH_SHORT).show();
//            UtilsData.getPreference(getActivity(), "carCount").put("car_pf_count", bean.getData().getCount().getPfcount());
//            EventBus.getDefault().post(new UpdateCarNumMessage());
//        } else {
//            if (bean.getStatus() == Constants.LOGIN_CODE) {
//                UtilsData.getPreference(getActivity(), "login").remove("head_url");
//                UtilsData.getPreference(getActivity(), "login").remove("nickname");
//                UtilsData.getPreference(getActivity(), "login").remove("is_store");
//                UtilsData.getPreference(getActivity(), "login").remove("token");
//                APP_TOKEN = "";
//                Intent intent = new Intent(getActivity(), OneLoginMainActivity.class);
//                startActivity(intent);
//                return;
//            }
//            Toast.makeText(getActivity(), bean.getMsg(), Toast.LENGTH_SHORT).show();
//        }
//    }
//
//    //自定义的图片加载器
//    private class MyLoader extends ImageLoader {
//        @Override
//        public void displayImage(Context context, Object path, ImageView imageView) {
//            RoundedCorners roundedCorners = new RoundedCorners(40);
//            //通过RequestOptions扩展功能,override:采样率,因为ImageView就这么大,可以压缩图片,降低内存消耗
////         RequestOptions options = RequestOptions.bitmapTransform(roundedCorners).override(300, 300);
//            RequestOptions options = RequestOptions.bitmapTransform(roundedCorners);
//            options.placeholder(R.drawable.icon_defult)//图片加载出来前，显示的图片
//                    .fallback(R.drawable.icon_defult) //url为空的时候,显示的图片
//                    .error(R.drawable.icon_defult);
//
//            imageView.setPadding(50, 0, 50, 0);
//
//            Glide.with(context).load(path).apply(options.transform(new CenterCrop(), roundedCorners)).into(imageView);
//        }
//
//    }
//
//    public static HomeMarketFragment newInstance() {
//        Bundle bundle = new Bundle();
////        bundle.putString("item", "UserPostFragment");
////        bundle.putLong(Constants.UID, userUid);
//        HomeMarketFragment fragment = new HomeMarketFragment();
//        fragment.setArguments(bundle);
//        return fragment;
//    }
//
//    @Override
//    public void finishRefresh() {
//        mRefreshLayout.finishRefresh(true);//刷新完成
//    }
//
//    @Override
//    public void finishLoadMore() {
//        mRefreshLayout.finishLoadMore(true);//加载完成
//    }
//
//    @Override
//    public void OnBannerClick(int position) {
//
//    }
//
//    @Override
//    public void onDestroy() {
//        if (EventBus.getDefault().isRegistered(this)) {
//            EventBus.getDefault().unregister(this);
//        }
//        super.onDestroy();
//    }
//}
